<?php

namespace App\Http\Controllers\Home;


use App\Model\buy;
use App\Model\ssq;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SsqController extends Controller
{
    public function index(){
        $qh_obj = (new ssq())->getLastQh();
        $qihao = $qh_obj->qihao;
        $buy_ssq[$qihao] = (new buy())->getLastQh($qihao);
        $qihao++;
        $buy_ssq[$qihao] = (new buy())->getLastQh($qihao);
        return view('ssq',compact('qihao','qh_obj','buy_ssq'));

    }

    public function handle(Request $request){
        $info = $request -> post();

        for($i=1;$i<6;$i++){
            if($info['no'.$i][6]){
                $info['no'.$i] = implode($info['no'.$i],"|");
            }else{
                $info['no'.$i] = null;
            }
        }

        $res = buy::create($info);

        if($res){
            $abc = "success";
        }else{
            $abc = "fail";
        }

        $qh_obj = (new ssq())->getLastQh();
        $qihao = $qh_obj->qihao;
        $buy_ssq[$qihao] = (new buy())->getLastQh($qihao);
        $qihao++;
        $buy_ssq[$qihao] = (new buy())->getLastQh($qihao);
        return view('ssq',compact('abc','qihao','qh_obj','buy_ssq'));
    }

    public function copy()
    {
        // 1. 获取有效日期数据（假设返回的是模型实例或 stdClass）
        $res = (new buy())->getValidDate();

        // 2. 获取最新的期号
        $qh_obj = (new ssq())->getLastQh();
        $qihao = $qh_obj->qihao;

        // 3. 构造数组（兼容对象或 stdClass）
        $resArray = is_object($res) ? ($res->toArray() ?? (array) $res) : (array) $res;

        // 4. 设置新的期号
        $resArray['qihao'] = $qihao + 1;

        // 5. 创建新记录
        $newRecord = buy::create($resArray);

        // 6. 判断是否成功
        return $newRecord ? "success" : "fail";
    }
}
